package flc.ast.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.stark.more.MorePrefUtil;
import com.stark.more.about.DefAboutActivity;

import java.util.List;

import flc.ast.R;
import flc.ast.activity.CollectActivity;
import flc.ast.activity.SettingActivity;
import flc.ast.databinding.FragmentMyBinding;
import flc.ast.dialog.SetAnswerDialog;
import flc.ast.dialog.SetPasswordDialog;
import flc.ast.utils.AppSpUtils;
import flc.ast.utils.Content;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.SPUtil;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MyFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MyFragment extends BaseNoModelFragment<FragmentMyBinding> {


    @Override
    protected int onCreate() {
        return R.layout.fragment_my;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.tvCollect.setOnClickListener(this);
        mDataBinding.ivSetting.setOnClickListener(this);
        mDataBinding.ivChangePwd.setOnClickListener(this);
        mDataBinding.ivFeedback.setOnClickListener(this);
        mDataBinding.ivPrivacy.setOnClickListener(this);
        mDataBinding.ivAbout.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvCollect://收藏
                startActivity(CollectActivity.class);
                break;
            case R.id.ivSetting://设置
                startActivity(SettingActivity.class);
                break;
            case R.id.ivChangePwd://修改密码
                forgetPasswordDialog();
                break;
            case R.id.ivFeedback://意见反馈
                BaseWebviewActivity.openFeedback(getActivity());
                break;
            case R.id.ivPrivacy://隐私政策
                BaseWebviewActivity.openAssetPrivacy(getActivity());
                break;
            case R.id.ivAbout://关于我们
                startActivity(new Intent(getActivity(), DefAboutActivity.class));
                break;
        }
    }

    @Override
    protected void initData() {
        boolean showPersonalRecommend = MorePrefUtil.showPersonalRecommend();
        if (showPersonalRecommend) {
            //显示个性化开关控件
            mDataBinding.ivSetting.setVisibility(View.VISIBLE);
        } else {
            //隐藏个性化开关控件
            mDataBinding.ivSetting.setVisibility(View.GONE);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        List<StkResBean> listSp = AppSpUtils.getCollectList();
        if (listSp != null && listSp.size() > 0) {
            mDataBinding.tvCollectNum.setText(getString(R.string.collect_num, listSp.size()));
        } else {
            mDataBinding.tvCollectNum.setText(getString(R.string.collect_num, 0));
        }
    }

    private void forgetPasswordDialog() {
        SetAnswerDialog setAnswerDialog = new SetAnswerDialog(mContext);
        setAnswerDialog.isForget = true;
        setAnswerDialog.setListener(new SetAnswerDialog.Listener() {
            @Override
            public void setPassword(String question, String answer) {
                changePasswordDialog();
            }
        });
        setAnswerDialog.show();
    }

    private void changePasswordDialog() {
        SetPasswordDialog setPasswordDialog = new SetPasswordDialog(mContext);
        setPasswordDialog.setListener(new SetPasswordDialog.Ilistener() {
            @Override
            public void click(String password) {
                SPUtil.putString(getContext(), Content.myPassword, password);
            }
        });
        setPasswordDialog.show();
    }


}